package com.jzx.service.impl;

import com.jzx.bean.Information;
import com.jzx.bean.User;
import com.jzx.dao.InfoDao;
import com.jzx.service.InfoService;
import com.jzx.service.UserService;
import com.jzx.utils.InfoAddressUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JunedQQ
 * @create 2019/6/24
 * @since 1.0.0
 */
@Service("infoService")
public class InfoServiceImpl implements InfoService {

    @Autowired
    private InfoDao infoDao;

    @Autowired
    private UserService userService;
    @Override
    public List<Information> getAllInfo() {
        List<Information> infos = infoDao.getAllInfo();
        //获取用户id
        for (int i = 0 ;i<infos.size();i++){
            //根据案例id查询用户id
            String userId = getUserIdById(infos.get(i).getId()+"");
            //获取到用户信息
            User user = userService.findUserById(userId);
            //设置密码为空
            user.setPassword(null);
            //设置作者信息
            infos.get(i).setAuthor(user);
        }
        //获取资讯的图片地址
        for (int i = 0;i<infos.size();i++){
            //查询获取资讯图片地址
            List<String> photos = getInfoPhotoAddressById(infos.get(i).getId()+"");
            //设置图片地址
            infos.get(i).setAddress(photos);
        }
        return infos;
    }

    @Override
    public Information getInfoById(String id) {
        Information info = infoDao.getInfoById(id);
        //获取userid
        String userId = getUserIdById(info.getId()+"");
        //获取作者信息
        User user = userService.findUserById(userId);
        //设置密码为空
        user.setPassword(null);
        //设置作者信息
        info.setAuthor(user);
        //查询获取资讯图片地址
        List<String> photos = getInfoPhotoAddressById(info.getId()+"");
        //设置图片地址
        info.setAddress(photos);
        return info;
    }

    @Override
    public List<Information> getInfoByUserId(String userId) {
        List<Information> infos = infoDao.getInfoByUserId(userId);
        //获取用户id
        for (int i = 0 ;i<infos.size();i++){
            //获取到用户信息
            User user = userService.findUserById(userId);
            //设置密码为空
            user.setPassword(null);
            //设置作者信息
            infos.get(i).setAuthor(user);
        }
        //获取资讯的图片地址
        for (int i = 0;i<infos.size();i++){
            //查询获取资讯图片地址
            List<String> photos = getInfoPhotoAddressById(infos.get(i).getId()+"");
            //设置图片地址
            infos.get(i).setAddress(photos);
        }
        return infos;
    }

    @Override
    public int getInfoSeeById(String id) {
        return infoDao.getInfoSeeById(id);
    }

    @Override
    public String getInfoTitleById(String id) {
        return infoDao.getInfoTitleById(id);
    }

    @Override
    public List<String> getInfoPhotoAddressById(String id) {
        String address =infoDao.getInfoPhotoAddressById(id);
        return InfoAddressUtils.string2List(address);
    }

    @Override
    public int addInformation(Information info) {
        String address = InfoAddressUtils.list2String(info.getAddress());
        return infoDao.addInformation(info,address);
    }

    @Override
    public int deleteInfoById(String id) {
        return infoDao.deleteInfoById(id);
    }

    @Override
    public int deleteInfoByUserId(String userId) {
        return infoDao.deleteInfoByUserId(userId);
    }

    @Override
    public int updateInfoUserIdById(String id, String userId) {
        return infoDao.updateInfoUserIdById(id,userId);
    }

    @Override
    public int infoSeePlusOne(String id) {
        return infoDao.infoSeePlusOne(id);
    }

    @Override
    public int updateInfoTitleById(String id, String title) {
        return infoDao.updateInfoTitleById(id,title);
    }

    @Override
    public int updateInfoPhotoAddressById(String id, String address) {
        return infoDao.updateInfoPhotoAddressById(id,address);
    }

    @Override
    public String getUserIdById(String id) {
        return infoDao.getUserIdById(id);
    }

    @Override
    public List<Information> getTenInfo(int start) {
        List<Information> infos = infoDao.getTenInfo(start);
        //获取用户id
        for (int i = 0 ;i<infos.size();i++){
            String userId = getUserIdById(infos.get(i).getId()+"");
            //获取到用户信息
            User user = userService.findUserById(userId);
            //设置密码为空
            user.setPassword(null);
            //设置作者信息
            infos.get(i).setAuthor(user);
        }
        //获取资讯的图片地址
        for (int i = 0;i<infos.size();i++){
            //查询获取资讯图片地址
            List<String> photos = getInfoPhotoAddressById(infos.get(i).getId()+"");
            //设置图片地址
            infos.get(i).setAddress(photos);
        }
        return infos;
    }

    @Override
    public List<Information> getAnyInfo(int start, int count) {
        List<Information> infos = infoDao.getAnyInfo(start,count);
        //获取用户id
        for (int i = 0 ;i<infos.size();i++){
            String userId = getUserIdById(infos.get(i).getId()+"");
            //获取到用户信息
            User user = userService.findUserById(userId);
            //设置密码为空
            user.setPassword(null);
            //设置作者信息
            infos.get(i).setAuthor(user);
        }
        //获取资讯的图片地址
        for (int i = 0;i<infos.size();i++){
            //查询获取资讯图片地址
            List<String> photos = getInfoPhotoAddressById(infos.get(i).getId()+"");
            //设置图片地址
            infos.get(i).setAddress(photos);
        }
        return infos;
    }
}